<?php
if(!isset($_SESSION)) 
{ 
	session_start(); 
}
unset($_SESSION['loadCheckTimes']);
if (isset($_REQUEST['dateupdate'])) {
	include 'CheckTime.php';
	include '../operations/connection.php';
	//update checktime
	$idupdate= $_REQUEST['idupdate'];
	$dateupdate= $_REQUEST['dateupdate'];
	$inupdate= $_REQUEST['inupdate'];
	$outupdate= $_REQUEST['outupdate'];
	$noteupdate= $_REQUEST['noteupdate'];
	
	
	$obUpdate = new CheckTime();
	$obUpdate->setDateUpdate($dateupdate);
	$obUpdate->inTime($inupdate);
	$obUpdate->outTime($outupdate);
	$obUpdate->note = $noteupdate;
	$obUpdate->off($inupdate, $outupdate);
	$userUpdate = $_SESSION['IDEmployee'];
	
	$updateSQL = sprintf("update sa_check_time set time_work='%s',time_in='%s',time_out='%s',is_late=%s,is_comeback_early=%s,half_day_off=%s,all_day_off=%s,overtime=%s,note='%s',user_update=%s,update_date=NOW() where id=%s", 
	$obUpdate->date,$obUpdate->inTime,$obUpdate->outTime,$obUpdate->late,$obUpdate->early,$obUpdate->halfDayOff,$obUpdate->aDayOff,$obUpdate->overtime,$obUpdate->note,$userUpdate,$idupdate);
	mysql_query($updateSQL);
	
	//reload checktime
	$searchload = $_SESSION['searchload'];
//	$sql = sprintf("select ct.id,ct.employeeID,ct.employeeName,ct.time_work,ct.time_in,ct.time_out,ct.is_late,ct.is_comeback_early,ct.half_day_off,ct.all_day_off,ct.overtime,ct.note,ct.user_create,ct.createDate
//from sa_check_time ct, employee em where  ct.employeeID = em.EmployeeCode and ct.`status`=0 and '%s' <= ct.time_work and ct.time_work <= '%s' and em.FullName like '%s' order by time_work asc",$searchload['datefrom'],$searchload['dateto'],'%'.$searchload['employeename'].'%') ;
	$sql = sprintf("select em.IDEmployee as emid,
	em.FullName as emname,
	ct.id,
	ct.employeeID,
	upsuser.FullName as upname ,
	cruser.FullName as crname ,
	ct.employeeName,ct.time_work,
	ct.time_in,ct.time_out,
	ct.is_late,
	ct.is_comeback_early,
	ct.half_day_off,
	ct.all_day_off,
	ct.overtime,
	ct.note,
	ct.user_create,ct.createDate,
	ct.user_update,
	ct.update_date,ct.`status` as stct 
from sa_check_time ct left join employee em on ct.employeeID = em.EmployeeCode 
left join  employee upsuser on upsuser.IDEmployee = ct.user_update 
left join employee cruser on cruser.IDEmployee = ct.user_create 
where ct.`status`!=1 and '%s' <= ct.time_work 
and ct.time_work <= '%s' 
and em.FullName like '%s' order by ct.employeeID asc",$searchload['datefrom'],$searchload['dateto'],'%'.$searchload['employeename'].'%') ;
	//echo $sql;
	$list = mysql_query($sql);
	$objects = array();
	while($row = mysql_fetch_array($list)){
		$object = new CheckTime();
		$object->Instance($row[id],$row[employeeID], $row[emname], $row[time_work], $row[time_in], $row[time_out], $row[is_late],
		$row[is_comeback_early], $row[half_day_off], $row[all_day_off], $row[overtime], $row[note]);
		if($row[user_update]==""){
			$object->setUpdateInfo($row[crname],$row[createDate]);
		}else{
			$object->setUpdateInfo($row[upname],$row[update_date]);
		}
		$object->setIs_status($row[stct]);
		array_push($objects, $object);
	}
	$_SESSION['loadCheckTimes'] = $objects;
	mysql_close($connection);
	header("Location: ../salary.php?content=working_spreadsheet");
}
?>